package com.ftg.shopping.sp_roleinfo.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.shopping.config.LayuiPage;
import com.ftg.shopping.sp_roleinfo.entity.SpRoleinfo;
import com.ftg.shopping.sp_roleinfo.mapper.RoleMapper;
import com.ftg.shopping.sp_roleinfo.mapper.SpRoleprivilegeMidMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    SpRoleprivilegeMidMapper sys_roleprivilege_midMapper;

    public List<SpRoleinfo> list(){ return roleMapper.selectList(null); }

    public SpRoleinfo findone(int roleid) {
        return roleMapper.selectById(roleid);
    }

    public int insertrole(SpRoleinfo r){
        return roleMapper.insert(r);
    }

    public int updaterole(SpRoleinfo r){ return roleMapper.updateById(r); }

    public int deleterole(int roleid){
        return  roleMapper.deleteById(roleid);
    }

    public int deleteroleIds(List<Integer> ids){
        return  roleMapper.deleteBatchIds(ids);
    }

    public IPage<SpRoleinfo> testPage(int page, int limit){
        IPage<SpRoleinfo> ip = new Page<>(page,limit);
        return roleMapper.selectPage(ip,null);
    }


    public LayuiPage<SpRoleinfo> testPage1(int page, int limit, String rolename){
        IPage<SpRoleinfo> ip = new Page<>(page,limit);
        QueryWrapper qw = new QueryWrapper();
        qw.like("goodsname",rolename);
        roleMapper.selectPage(ip,rolename ==null || rolename.equals("") ? null:qw);
        LayuiPage role=new LayuiPage(ip);
        return role;
    }

    public LayuiPage<SpRoleinfo> testPage2(int page, int limit){

        IPage<SpRoleinfo> ip = new Page<>(page,limit);

        roleMapper.selectPage(ip,null);

        return new LayuiPage<>(ip) ;
    }
    public Map<String,Object> quanxian2(int roleId) {
        Map<String,Object> map = new HashMap<>();

        List<Map<String, Object>> list = roleMapper.quanxianall();
        List<Map<String, Object>> self =  roleMapper.quanxian(roleId);

        map.put("all",list);
        map.put("self",self);

        return map;

    }

    @Transactional(rollbackFor = Exception.class)
    public int quanxian3(int roleId, List<Integer> pids) {
        sys_roleprivilege_midMapper.deleteById(roleId);

        for(int i = 0 ;i < pids.size();i++){
            sys_roleprivilege_midMapper.insertid(roleId,pids.get(i));
        }
        return 1;
    }
}
